package com.yuexinduo.education.ui;

import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.RequiresApi;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.yuexinduo.education.R;
import com.yuexinduo.education.adapter.CourseAdapter;
import com.yuexinduo.baselibrary.base.BaseMvpActivity;
import com.yuexinduo.education.fragment.CourseAllFragment;
import com.yuexinduo.education.fragment.CourseUnconfirmedFragment;
import com.yuexinduo.education.fragment.CourseUpcomingFragment;

import java.util.ArrayList;
import java.util.List;

public class MyCourseActivity extends BaseMvpActivity implements View.OnClickListener {
    private ImageView back;
    private TextView top;
    private TextView courseAll;
    private TextView courseUpcoming;
    private TextView courseUnconfirmed;
    private ViewPager viewPager;
    private List<Fragment> fragmentList = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                switch (position){
                    case 0:
                        courseAll.setTextColor(getResources().getColor(R.color.red));
                        courseUpcoming.setTextColor(getResources().getColor(R.color.black));
                        courseUnconfirmed.setTextColor(getResources().getColor(R.color.black));
                        break;
                    case 1:
                        courseAll.setTextColor(getResources().getColor(R.color.black));
                        courseUpcoming.setTextColor(getResources().getColor(R.color.red));
                        courseUnconfirmed.setTextColor(getResources().getColor(R.color.black));
                        break;
                    case 2:
                        courseAll.setTextColor(getResources().getColor(R.color.black));
                        courseUpcoming.setTextColor(getResources().getColor(R.color.black));
                        courseUnconfirmed.setTextColor(getResources().getColor(R.color.red));
                        break;
                }


            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Override
    protected int bindLayout() {
        return R.layout.activity_my_course;
    }

    @Override
    protected Activity getActivity() {
        return this;
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    @Override
    protected void initCreate() {

        back = (ImageView) findViewById(R.id.back);
        top = (TextView) findViewById(R.id.top);
        courseAll = (TextView) findViewById(R.id.course_all);
        courseUpcoming = (TextView) findViewById(R.id.course_upcoming);
        courseUnconfirmed = (TextView) findViewById(R.id.course_unconfirmed);
        viewPager = (ViewPager) findViewById(R.id.viewPager);
        courseAll.setOnClickListener(this);
        courseUpcoming.setOnClickListener(this);
        courseUnconfirmed.setOnClickListener(this);
        top.setText("我的课程");

        fragmentList.add(new CourseAllFragment());
        fragmentList.add(new CourseUpcomingFragment());
        fragmentList.add(new CourseUnconfirmedFragment());


        CourseAdapter courseAdapter = new CourseAdapter(getSupportFragmentManager(), FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT, fragmentList);
        viewPager.setAdapter(courseAdapter);

    }

    @Override
    public void onClick(View v) {
       switch (v.getId()){
           case R.id.back:
               finish();
               break;
           case R.id.course_all:
               viewPager.setCurrentItem(0);
               break;
           case R.id.course_upcoming:
               viewPager.setCurrentItem(1);
               break;
           case R.id.course_unconfirmed:
               viewPager.setCurrentItem(2);
               break;
       }
    }

}